<?php
namespace BoxPay\Controller;

use Think\Controller;

class OrderController extends \HomeController
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        /*页面上通过表单选择在线支付类型，支付宝为alipay 财付通为tenpay*/
        header("Content-type:text/html;charset=utf-8");
        $order_sn = I("order_sn");
        $payment_id = I('payment_id', "0", "intval");
        $Agent = checkAgent();
        if (!$Agent && $payment_id == 0) {////微信浏览器
            $payment_id = "44";
            $url = HomeUrl . "/BoxPay/Order/index/order_sn/" . $order_sn . ".html" . "?payment_id=" . $payment_id;
            $str = '<div data-page="' . $this->pageName . '" class="page" ><script>window.location.href="' . $url . '"</script></div>';
            //dump();
            exit($str);
        }
        if ($payment_id) {
            // $payment_id=41;
            BoxModel("SystemOrder")->where(array('order_sn' => $order_sn))->save(array('payment_id' => $payment_id));
        }


        //		dump($order_sn);
        $orderInfo = BoxModel("SystemOrder")->where(array('order_sn' => $order_sn))->find();
        //	dump($orderInfo);exit();
        if (!$orderInfo) exit('订单不存在');
        if ($orderInfo['status'] != 1) exit('当前订单状态不可支付,订单状态：' . $orderInfo['status']);
        $payment_id = $orderInfo['payment_id'];
        //如果是微信浏览器,设置openid
        if (!$Agent && $payment_id) {
            $user_info = BoxModel("system_member")->where(array('user_id' => $orderInfo['user_id']))->find();
            $_GET['openid'] = $user_info['openid'];
        }

        /******回调的url*******/
        $url = $orderInfo['suceess_url'];
        /*****支付回调失败url*****/
        $sign = md5(ksort($orderInfo));
        $fail_url = HomeUrl . "/BoxPay/Index/fail/order_sn/" . $order_sn . "/sign/" . $sign;
        //dump($orderInfo);
        /***标题***/
        $title = $orderInfo['title'];
        /*****支付配置*****/
        $data = BoxModel("SystemPayment")->load($payment_id);
        $configData = json_decode($data['config'], true);
        /******如果没有支付方式**/
        if (!$data) {
            $this->orderInfo = $orderInfo;
            //dump( $this->orderInfo);
            $this->display();
            exit();
        }
        /*****在微信浏览器内不能使用支付宝***/

        if ((!$Agent) && $data['payment_code'] == "Wapalipay") {
            $this->display("Pay/tip");
            exit("由于微信中不支持支付宝支付，需要在浏览器中打开本页面完成支付，谢谢支持^_^o~ 努力！");
        }
        if (!$data) exit('支付方式不合法');
        /***货到付款***/
        if ($data['payment_code'] == "hdfk") {
            session("pay_verify", true);
            $this->succeed($order_sn);
            $this->jump("下单成功", $url);
            exit();
        }
        $paytype = $data['payment_code'];
        $setBody = $orderInfo['body'];
        /****支付宝担保交易邮费与商品费用分开****/
        if ($paytype == "Alipayensure") {
            $amount = $orderInfo['goods_amount'];
        } else {
            $amount = $orderInfo['order_amount'];
        }
        $Callback = HomeUrl . "/BoxPay/Index/notify";
        /****第三方结算****/
        $pay = new \Common\Org\Pay($paytype, $configData);
        // dump($pay);exit();
        $order_no = $pay->createOrderNo();
        $vo = new \Common\Org\Pay\PayVo();
        $vo->setBody($setBody)
            ->setFee($amount)/*支付金额*/
            ->setOrderNo($orderInfo['order_sn'])
            ->setTitle($title)
            ->setCallback($Callback)
            ->setUrl($url)
            ->setUserId($orderInfo['user_id'])
            ->setParam(
                array(
                    'store_id' => $orderInfo['store_id'],
                    'logistics_fee' => $orderInfo['logistics_fee'],
                    'suceess_url' => $url,//逻辑处理成功之后跳转url
                    'fail_url' => $fail_url,//支付失败ur
                )
            );
        $r = $pay->buildRequestForm($vo);
        $FormContent = '<div data-page="' . $this->pageName . '" class="page" >' . $r . '</div>';
        echo $FormContent;
        //dump($FormContent);exit();
        //$this->display("FormContent");
    }


    /**
     * 订单支付成功
     * @param type $money
     * @param type $param
     */
    public function succeed($order_sn)
    {
        $data = BoxModel("SystemOrder")->where(array('order_sn' => $order_sn))->find();
        if (session("pay_verify") == true) {
            session("pay_verify", null);
            /*********支付成功订单状态修改*********/
            session("pay_sucess", true);
            BoxModel("SystemOrder")->paySuc($order_sn);
        } else {
            E("Access Denied");
        }
    }

    function refreshOrder()
    {
        $order_sn = I('order_sn');
        $data = BoxModel("SystemOrder")->where(array('order_sn' => $order_sn))->find();
        if ($data['status'] != 1) {
            exit("1");
        } else {
            exit("0");
        }
    }
}

?>